package com.sqsf.util;

import com.sqsf.javabean.Permission;
import com.sqsf.javabean.Role;
import mapper.RoleMapper;
import mapper.UserMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

/**
 * @Author: CHENG
 * @CreateTime: 2025-03-13
 * @Description:
 */
public class RoleTest {
    SqlSession session;
    RoleMapper mapper;
    @Before
    public void a() {
        //1、创建SqlSessionFactory对象，也是单例模式的
        SqlSessionFactory factory = MyBatisUtil.getSqlSessionFactory();
        //2、创建SqlSession对象 true 自动提交事务 不写也没事 因为默认在sqlsession关闭时提交事务
        session = factory.openSession(true);
        //3、调用session的方法namespace的名字.id
        mapper = session.getMapper(RoleMapper.class);
    }

    @After
    public void b() {
        //5、关闭资源
        session.close();
    }

    @Test
    public void test() {

        for (Role role : mapper.SelectRole_Permission()) {
            System.out.println(role);
        }

    }
    @Test
    public void test1() {
        List<Role> roles = mapper.selectRole_Permission_lazy();
        //方法执行默认查询主表数据并返回
        //当使用主表数据时 可以直接使用
        Role role = roles.get(1);
//        System.out.println(role.getRid());
//        System.out.println(role.getName());
//        System.out.println(role.getDesc());
//        System.out.println("~~~~~~~~~~~~~~~~~~~~");
//        //当使用关联表数据时 会根据当前主表数据进行查询
//        System.out.println(role.getPermissions());

    }
}
